package project.vip.services.impl;

import java.io.Serializable;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import kernel.util.StringUtils;
import kernel.web.Page;
import kernel.web.PagedQueryDao;

import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import project.order.Order;
import project.vip.po.Ad;
import project.vip.po.Vip;
import project.vip.services.AdminVipService;

public class AdminVipServiceImpl extends HibernateDaoSupport implements AdminVipService {

    private PagedQueryDao pagedQueryDao;

    @Override
    public List<Vip> getVipList(String phone) {
        StringBuffer queryString = new StringBuffer(" FROM Vip WHERE phone = ? ORDER BY createTime");
        return this.getHibernateTemplate().find(queryString.toString(),new Object[]{phone});
    }

    @Override
    public Page getSendInvList(int pageNo, int pageSize, String name, String phone, Date createTime_begin,
            Date createTime_end,String toOpenId,String fromOpenId) {

        StringBuffer queryString = new StringBuffer(" FROM Inv WHERE  ");

        Map<String, Object> parameters = new HashMap<String, Object>();

        if (!StringUtils.isNullOrEmpty(fromOpenId)) {
            queryString.append(" fromOpenId  = :fromOpenId");
            parameters.put("fromOpenId", fromOpenId);
        }
        
        if (!StringUtils.isNullOrEmpty(toOpenId)) {
            queryString.append(" toOpenId  = :toOpenId");
            parameters.put("toOpenId", toOpenId);
        }
        if (!StringUtils.isNullOrEmpty(phone)) {
            queryString.append(" and phone  = :phone");
            parameters.put("phone", phone);
        }
        if (!StringUtils.isNullOrEmpty(name)) {
            queryString.append(" and name  = :name");
            parameters.put("name", name);
        }

        if (createTime_begin != null) {
            queryString.append(" and createTime >=   :createTime_begin");
            parameters.put("createTime_begin", createTime_begin);

        }
        if (createTime_end != null) {
            queryString.append(" and createTime <=   :createTime_end");
            parameters.put("createTime_end", createTime_end);

        }

        queryString.append(" order by createTime desc ");
        Page page = pagedQueryDao.pagedQuery(pageNo, pageSize, queryString.toString(), parameters);
        return page;
    }

    @Override
    public Order findOrder(Serializable id) {

        StringBuffer queryString = new StringBuffer(" FROM Order where id=?");
        List<Order> list = this.getHibernateTemplate().find(queryString.toString(), id);
        if (list.size() > 0) {
            return list.get(0);
        }
        return null;
    }
    @Override
    public Page myOrder(int pageNo, int pageSize, String name, String phone, Date createTime_begin,
            Date createTime_end,String openId) {

        StringBuffer queryString = new StringBuffer(" FROM Order WHERE  ");

        Map<String, Object> parameters = new HashMap<String, Object>();
        if (!StringUtils.isNullOrEmpty(openId)) {
            queryString.append(" openId  = :openId");
            parameters.put("openId", openId);
        }
        if (!StringUtils.isNullOrEmpty(phone)) {
            queryString.append(" and phone  = :phone");
            parameters.put("phone", phone);
        }
        if (!StringUtils.isNullOrEmpty(name)) {
            queryString.append(" and name  = :name");
            parameters.put("name", name);
        }

        if (createTime_begin != null) {
            queryString.append(" and createTime >=   :createTime_begin");
            parameters.put("createTime_begin", createTime_begin);

        }
        if (createTime_end != null) {
            queryString.append(" and createTime <=   :createTime_end");
            parameters.put("createTime_end", createTime_end);

        }

        queryString.append(" order by createTime desc ");
        Page page = pagedQueryDao.pagedQuery(pageNo, pageSize, queryString.toString(), parameters);
        return page;
    }
    
    @Override
    public Vip find(String openid) {
        StringBuffer queryString = new StringBuffer(" FROM Vip where openId=?");
        List<Vip> list = this.getHibernateTemplate().find(queryString.toString(), openid);
        if (list.size() > 0) {
            return list.get(0);
        }
        return null;
    }
    @Override
    public Page pagedQuery(int pageNo, int pageSize, String phone) {
        StringBuffer queryString = new StringBuffer(" FROM Vip WHERE 1=1 ");

        Map<String, Object> parameters = new HashMap<String, Object>();
        if (!StringUtils.isNullOrEmpty(phone)) {
            queryString.append(" and phone  = :phone");
            parameters.put("phone", phone);
        }
        queryString.append(" order by createTime desc ");
        Page page = pagedQueryDao.pagedQuery(pageNo, pageSize, queryString.toString(), parameters);
        return page;

    }

    @Override
    public Vip getById(Serializable id) {
        StringBuffer queryString = new StringBuffer(" FROM Vip where id=?");
        List<Vip> list = this.getHibernateTemplate().find(queryString.toString(), id);
        if (list.size() > 0) {
            return list.get(0);
        }
        return null;
    }

    @Override
    public void save(Vip entity) {
        entity.setCreateTime(new Date());
        this.getHibernateTemplate().save(entity);

    }

    @Override
    public void update(Vip entity) {
        this.getHibernateTemplate().update(entity);

    }
    @Override
    public void delete(Serializable id) {

    	Vip en = this.getHibernateTemplate().get(Vip.class, id);
        if (en != null) {
        	this.getHibernateTemplate().delete(en);
        }
    }
    
    public void setPagedQueryDao(PagedQueryDao pagedQueryDao) {
        this.pagedQueryDao = pagedQueryDao;
    }

}
